package org.example.controller;

import org.example.dto.GenderCheckBox;
import org.example.dto.Score;
import org.example.dto.SimplePoiTlTemplateDto;
import org.example.dto.StudentInfo;
import org.example.service.PoiTlService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

@RequestMapping("test")
@RestController
public class TestController {

    private static final String STUDENT_FILE_TEMPLATE_PATH = "/templates/student-file.docx";

    private static final String STUDENT_FILE_NAME = "学生档案.docx";

    @Resource
    private PoiTlService poiTlService;

    @GetMapping
    public void test(HttpServletResponse response) throws IOException {

        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(STUDENT_FILE_NAME, "UTF-8").replaceAll("\\+", "%20"));

        List<Score> scoreList = Arrays.asList(
                new Score().setResult(97).setSubject("语文"),
                new Score().setResult(98).setSubject("英语"),
                new Score().setResult(100).setSubject("数学")
        );
        SimplePoiTlTemplateDto templateDto = new SimplePoiTlTemplateDto()
                .setStudentInfo(new StudentInfo().setAddress("湖北武汉").setName("小张"))
                .setGenderCheckBox(GenderCheckBox.of(0))
                .setScoreList(scoreList);
        poiTlService.write(
                response.getOutputStream(),
                templateDto,
                this.getClass().getResourceAsStream(STUDENT_FILE_TEMPLATE_PATH)
        );
    }


}
